package com.fjut.zr.judge_system.VJ.VJS;

import com.fjut.zr.judge_system.POJO.PO.TVjResultPO;
import com.fjut.zr.judge_system.Util.MyClient;
import com.fjut.zr.judge_system.Util.POJCodeLanguage;
import com.fjut.zr.judge_system.VJ.VjSubmitter;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
created by zhuoren on 2021/8/18
 */

public class POJ extends BaseVJ {
    public static Map<String, String> ResultMap;

    static {
        ResultMap = new HashMap<String, String>();
        ResultMap.put("Accepted", "Accepted");
        ResultMap.put("Wrong Answer", "Wrong Answer");
        ResultMap.put("Presentation Error", "Presentation Error");
        ResultMap.put("Time Limit Exceeded", "Time Limit Exceeded");
        ResultMap.put("Memory Limit Exceeded", "Memory Limit Exceeded");
        ResultMap.put("Runtime Error", "Runtime Error");
        ResultMap.put("Output Limit Exceeded", "Output Limit Exceeded");
        ResultMap.put("Compile Error", "Compilation Error");
        ResultMap.put("Running & Judging", "JUDGING");
        ResultMap.put("Waiting", "JUDGING");
        ResultMap.put("Compiling", "JUDGING");
        ResultMap.put("System Error", "System Error");
        ResultMap.put("Validator Error", "ERROR");
    }


    @Override
    public String getRid(VjSubmitter s) {
        //获取评测ID
        Document document;
        String statusurl="http://poj.org/status?problem_id=&user_id=zhuoren&result=&language=";
        //解析网页（jsoup返回document就是浏览器里的document）连接超过十秒为超时
        try {
            document = Jsoup.parse(new URL(statusurl), 10000);
        } catch (IOException e) {
            return "error";
        }
        Element e = document.select(".a tr").get(1);
        return e.select("td:nth-child(1)").first().text();
    }

    @Override
    public String submit(VjSubmitter s) {
        String url= "http://poj.org";
        MyClient hc=new MyClient();
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("user_id1","zhuoren"));
        formparams.add(new BasicNameValuePair("password1","@ZR13559365988"));
        formparams.add(new BasicNameValuePair("url","/"));
        hc.Post(url+"/login", formparams);
        List<NameValuePair> formparams1 = new ArrayList<NameValuePair>();
        int languageid=new POJCodeLanguage().getLanguageId(s.info.getLanguage());
        formparams1.add(new BasicNameValuePair("language",Integer.toString(languageid)));
        formparams1.add(new BasicNameValuePair("problem_id",s.info.getPid()));
//        String code= new BASE64Encoder().encode(s.info.getCode().getBytes());
        String code= new BASE64Encoder().encode(s.info.getCode().getBytes());
        formparams1.add(new BasicNameValuePair("source",code));
        formparams1.add(new BasicNameValuePair("encoded","1"));

        if(hc.Post(url+"/submit",formparams1)==null) {
            return "error";
        }
        else
        {
            return "success";
        }
    }

    @Override
    public TVjResultPO getResult(VjSubmitter s) {
        TVjResultPO resultInfo=new TVjResultPO();
        Document document = null;
        String statusurl="http://poj.org/status?problem_id=&user_id=zhuoren&result=&language=";
        //解析网页（jsoup返回document就是浏览器里的document）连接超过十秒为超时
        try {
            document= Jsoup.parse(new URL(statusurl),10000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Elements tableelements=document.getElementsByClass("a").eq(0);
        Elements elements = document.getElementsByTag("tr");
        for(Element tr:elements)
        {
            if(tr.getElementsByTag("td").eq(0).text().equals(s.getOjsrid())) {
                resultInfo.setRunId(tr.getElementsByTag("td").eq(0).text());
                resultInfo.setUser(tr.getElementsByTag("td").eq(1).text());
                resultInfo.setProblem(tr.getElementsByTag("td").eq(2).text());
                resultInfo.setResult(ResultMap.get(tr.getElementsByTag("td").eq(3).text()));
                resultInfo.setMemory(tr.getElementsByTag("td").eq(4).text());
                resultInfo.setTime(tr.getElementsByTag("td").eq(5).text());
                resultInfo.setLanguage(tr.getElementsByTag("td").eq(6).text());
                resultInfo.setCodelength(tr.getElementsByTag("td").eq(7).text());
                resultInfo.setSubmitTime(tr.getElementsByTag("td").eq(8).text());
                if(resultInfo.getResult().equals("Compilation Error"))
                {
                    resultInfo.setResultDetail(getCEInfo(resultInfo.getRunId()));
                }
                return resultInfo;
            }
        }
        return null;
    }
    public String getCEInfo(String runid){
        MyClient hc=new MyClient();
        Document d = hc.get("http://poj.org/showcompileinfo?solution_id="+runid);
        return d.select("pre").text();
    }

}
